package com.zys.sac.starter.initializer;

import com.zys.sac.core.filter.SacCheckFilter;
import com.zys.sac.core.helper.UrlRule;
import com.zys.sac.starter.config.SacPluginProperties;
import org.springframework.http.HttpMethod;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;

import java.util.List;

/**
 * sac特殊路径初始化器，将免登、只需登录的路径通过该初始化器初始化到过滤器中
 *
 * @author junekzhong
 * @date 2023/12/12 15:45
 */
public class SacSpecialPathInitializer implements SacFilterInitializer {

    private SacPluginProperties properties;

    public SacSpecialPathInitializer(SacPluginProperties properties) {
        this.properties = properties;
    }

    @Override
    public void init(SacCheckFilter filter) {
        // 无需任务检查的路径
        List<String> avoidPath = properties.getAvoidPath();
        if(null != avoidPath && avoidPath.size() > 0) {
            for(String path : avoidPath) {
                addAvoidPath(path, filter);
            }
        }
        // 只检查登录状态的路径（不做鉴权）
        List<String> onlyAuthPath = properties.getOnlyAuthPath();
        if (null != onlyAuthPath && onlyAuthPath.size() > 0) {
            for(String path : onlyAuthPath) {
                addOnlyAuthPath(path, filter);
            }
        }
        // url规则解析
        List<String> urlRules = properties.getUrlRule();
        if (null != urlRules && urlRules.size() > 0) {
            for (String urlRule : urlRules) {
                // url:op anon，auth，perm
                String[] split = urlRule.split(":");
                if (null != split && split.length == 2) {
                    String url = split[0];
                    String rule = split[1];
                    if ("anon".equals(rule)) {
                        addAvoidPath(url, filter);
                    } else if (rule.startsWith("auth")) {
                        addOnlyAuthPath(url, filter);
                    } else if (rule.startsWith("perm")) {
                        UrlRule ur = new UrlRule(url, rule);
                        filter.addUrlRule(ur);
                    } else {
                        throw new IllegalArgumentException("非法的路由规则!");
                    }
                }
            }
        }
    }

    private void addAvoidPath(String path, SacCheckFilter filter) {
        filter.addAvoidMatcher(new AntPathRequestMatcher(path, HttpMethod.GET.name()));
        filter.addAvoidMatcher(new AntPathRequestMatcher(path, HttpMethod.POST.name()));
        filter.addAvoidMatcher(new AntPathRequestMatcher(path, HttpMethod.OPTIONS.name()));
        filter.addAvoidMatcher(new AntPathRequestMatcher(path, HttpMethod.DELETE.name()));
        filter.addAvoidMatcher(new AntPathRequestMatcher(path, HttpMethod.PUT.name()));
    }

    private void addOnlyAuthPath(String path, SacCheckFilter filter) {
        filter.addOnlyLoginMatcher(new AntPathRequestMatcher(path, HttpMethod.GET.name()));
        filter.addOnlyLoginMatcher(new AntPathRequestMatcher(path, HttpMethod.POST.name()));
        filter.addOnlyLoginMatcher(new AntPathRequestMatcher(path, HttpMethod.OPTIONS.name()));
        filter.addOnlyLoginMatcher(new AntPathRequestMatcher(path, HttpMethod.DELETE.name()));
        filter.addOnlyLoginMatcher(new AntPathRequestMatcher(path, HttpMethod.PUT.name()));
    }

    @Override
    public int getOrder() {
        return Integer.MIN_VALUE;
    }
}
